package com.ruoyi.common.utils.poi;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.spire.doc.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordUtil {
    /**
     * 将上传的word文档保存到本地
     * @param file
     * @return
     * @throws IOException
     */
    public static String save(MultipartFile file) throws IOException {
        String path= FileUploadUtils.upload(file);
        String realPath=path.substring(path.indexOf("e")+2);
        String baseDir= RuoYiConfig.getProfile();
        String filePath=baseDir+realPath;
        return filePath;
    }

    /**
     * 读取文本内容
     * @param filePath
     * @return
     * @throws Exception
     */
    public String getText(String filePath) throws Exception{
        Document doc=new Document();
        doc.loadFromFile(filePath);
        String originalContent=doc.getText();
        return originalContent;
    }

    /**
     * 将读取到的内容封装
     * @param content
     */
    public Map<String,String> setResume(String content){
        Map<String,String> map=new HashMap<>();
        String skillRegex="职业技能\r?\n([^\r\n]+)";
        String experienceRegex="工作经验\r?\n([^\r\n]+)";
        String educationRegex="教育背景\r?\n([^\r\n]+)";
        String skill=regex(skillRegex,content);
        String experience=regex(experienceRegex,content);
        String education=regex(educationRegex,content);
        map.put("skills",skill);
        map.put("experience",experience);
        map.put("education",education);
        return map;
    }

    public String regex(String regex,String content){
        Pattern skillPattern=Pattern.compile(regex);
        Matcher skillMatcher=skillPattern.matcher(content);
        if(skillMatcher.find()){
            String data=skillMatcher.group(1);
            return data;
        }
        return null;
    }
}
